import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/product_productionorderdetail_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input_num.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';

class StockOut extends StatefulWidget {

  final List<Map<String, dynamic>> bomList;

  String plancode;

  StockOut({Key? key,required this.bomList,required this.plancode}): super(key: key);

  @override
  _StockOutState createState() => _StockOutState();
}

class _StockOutState extends State<StockOut>{

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    var table = DataTable(
      columns: [
        DataColumn(label: Text('物料编号')),
        DataColumn(label: Text("物料名称")),
        DataColumn(label: Text("规格型号")),
        DataColumn(label: Text("领料出库数量")),
        DataColumn(label: Text("计量单位")),
      ],
      rows: widget.bomList.map((map) {
        var result = DataRow(
            cells: [
              DataCell(Text(map["default_code"]!=false?map["default_code"].toString():"")),
              DataCell(Text(map["name"]!=false?map["name"].toString():"")),
              DataCell(Text(map["description"]!=false?map["description"].toString():"")),
              DataCell(CryInputNum(
                width: 200,
                padding: 0,
                contentPadding: 0,
                value: map["product_qty"],
                onChange: (v){
                  map["product_qty"] = v;
                },
              )),
              DataCell(Text(map["product_uom"]!=false?map["product_uom"].toString():"")),
            ]
        );
        return result;
      }).toList(),
    );

    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            var productCode = widget.bomList.map((e) => {"code":e["default_code"],"num":e["product_qty"]}).toList();
            var domain = [
              ['plancode', '=', widget.plancode],
              ['product_code', productCode]
            ];
            ProductProductionorderdetailOdooApi.stockOut(
                domain)
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              if(res.data["code"]!=200){
                CryUtils.message(res.data["message"]);
                return ;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("领料出库确认"),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 10),
          CryInput(
            label: "生产订单编码",
            enable: false,
            value: widget.plancode,
          ),
          SingleChildScrollView(
            child: table,
          )
        ],
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      // width: 650,
      height: 650,
      child: result,
    );
  }
}